﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace JetLag2011
{
    public enum Music
    {
        LeadIn,
        Song
    }
    public class MusicManager:MediaElementManager<Music>
    {
        private void MutedChanged(bool isMuted)
        {
            foreach (Music key in Keys)
            {
                this[key].IsMuted=isMuted;
            }
        }
        private void VolumeChanged(double newVolume)
        {
            foreach (Music key in Keys)
            {
                this[key].Volume = newVolume;
            }
        }
        private void MediaEnded(object sender, RoutedEventArgs e)
        {
            MediaElement element = sender as MediaElement;
            if (!Muted)
            {
                element.Position = new TimeSpan(0,0,0);
                element.Play();
            }
        }
        private void MediaElementAdded(Music theKey, MediaElement theMediaElement)
        {
            theMediaElement.IsMuted = Muted;
            theMediaElement.Volume = Volume;
            theMediaElement.MediaEnded += MediaEnded;
        }
        private void MediaElementChanged(Music theKey, MediaElement theOldMediaElement, MediaElement theNewMediaElement)
        {
            theOldMediaElement.Stop();
            theOldMediaElement.MediaEnded -= MediaEnded;
            theNewMediaElement.IsMuted = Muted;
            theNewMediaElement.Volume = Volume;
            theNewMediaElement.MediaEnded += MediaEnded;
        }
        public MusicManager():base(0.25,false)
        {
            OnMutedChanged += MutedChanged;
            OnVolumeChanged += VolumeChanged;
            OnMediaElementAdded += MediaElementAdded;
            OnMediaElementChanged += MediaElementChanged;
        }
    }
}
